<template>
<div class="pdf-preview " >
    <div class="pdf-wrap ">
        <vue-pdf-embed :source="state.source"  class="vue-pdf-embed" :page="state.pageNum" />
    </div>
    <div class=" justify-center">
        <n-pagination class=" justify-center " v-model:page="state.pageNum" :page-count="state.numPages" size="large"/>
    </div>
    </div>
</template>
<script setup lang="ts">
import { reactive, onMounted } from "vue";
import VuePdfEmbed from "vue-pdf-embed";
import { createLoadingTask } from "vue3-pdfjs/esm"; // 获得总页数

const props = defineProps({
    pdfUrl: {
        type: String,
        required: true
    }
})

const state = reactive({
    source: props.pdfUrl, //预览pdf文件地址
    pageNum: 1, //当前页面
    scale: 1, // 缩放比例
    numPages: 0, // 总页数
});


onMounted(() => {
    const loadingTask = createLoadingTask(state.source);
    loadingTask.promise.then((pdf: { numPages: number }) => {
        state.numPages = pdf.numPages;
    });
});

</script>
<style lang="css" scoped>
.pdf-preview {
    position: relative;
    height: 100%;
    padding: 20px 0;
    box-sizing: border-box;
}

.vue-pdf-embed {
    text-align: center;
    width: 800px;
    border: 1px solid #e5e5e5;
    margin: 0 auto;
    box-sizing: border-box;
}
</style>